package com.macro.mall.portal.controller;

import com.macro.mall.model.UmsMember;
import com.macro.mall.portal.constans.PathConstants;
import com.macro.mall.portal.domain.MemberDetails;
import com.macro.mall.portal.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.validation.Valid;
import java.security.Principal;

/**
 * @author super
 * @title: OrderController
 * @description: TODO
 * @date 2020/10/12 17:33
 */
@Slf4j
@RestController
@RequestMapping("/order")
@Api(tags = "orderController", description = "订单管理")
public class OrderController {
    @Autowired
    private RestTemplate restTemplate;

    @Value("${host.mall.order}")
    private String HOST_MALL_ORDER;


    @ApiOperation(value = "订单提交接口", notes = "订单提交接口", position = 1)
    @PostMapping(value = PathConstants.ODR_ORDRE_COMMIT,consumes = MediaType.APPLICATION_JSON_VALUE,produces = MediaType.APPLICATION_JSON_VALUE)
    public RespVO commit(/*@ApiParam(value = "业务流水号", required = true, name = "x-b3-traceId") @RequestHeader("x-b3-traceId") String traceId,
                         @ApiParam(value = "请求流水号", required = true, name = "x-b3-spanId") @RequestHeader("x-b3-spanId") String spanId,*/
            Principal principal, @RequestBody @ApiParam(value = "请求报文", required = true) @Valid OrderCommitReqVO request) {
        log.info("订单提交请求报文：{}",request);
        if(principal==null){
            return RespVO.unauthorized();
        }
        UmsMember currentMember = getCurrentMember();
        request.setMobile(currentMember.getUsername());
        String url = HOST_MALL_ORDER +PathConstants.ODR_ORDRE + PathConstants.ODR_ORDRE_COMMIT;
        ResponseEntity<RespVO> responseEntity = restTemplate.postForEntity(url, request, RespVO.class);
        return responseEntity.getBody();
    }

    @ApiOperation(value = "订单状态流转接口", notes = "订单状态流转接口", position = 2)
    @PostMapping(value = PathConstants.ODR_ORDRE_STATUS_UPDATE,consumes = MediaType.APPLICATION_JSON_VALUE,produces = MediaType.APPLICATION_JSON_VALUE)
    public RespVO statusUpdate(/*@ApiParam(value = "业务流水号", required = true, name = "x-b3-traceId") @RequestHeader("x-b3-traceId") String traceId,
                               @ApiParam(value = "请求流水号", required = true, name = "x-b3-spanId") @RequestHeader("x-b3-spanId") String spanId,*/
            Principal principal,@RequestBody @ApiParam(value = "请求报文", required = true) @Valid OrderStatusUpdateReqVO request) {
        log.info("订单提交请求报文：{}",request);
        if(principal==null){
            return RespVO.unauthorized();
        }
        SecurityContext ctx = SecurityContextHolder.getContext();
        Authentication auth = ctx.getAuthentication();
        MemberDetails memberDetails = (MemberDetails) auth.getPrincipal();
        UmsMember umsMember = memberDetails.getUmsMember();
        String id = umsMember.getId()+"";
        String username = umsMember.getUsername();
        request.setUserId(id);
        request.setUserName(username);
        String url = HOST_MALL_ORDER +PathConstants.ODR_ORDRE + PathConstants.ODR_ORDRE_STATUS_UPDATE;
        ResponseEntity<RespVO> responseEntity = restTemplate.postForEntity(url, request, RespVO.class);
        return responseEntity.getBody();
    }

    @ApiOperation(value = "订单详细信息查询", notes = "订单详细信息查询", position = 3)
    @PostMapping(value = PathConstants.ODR_ORDRE_DETAILS_QUERY,consumes = MediaType.APPLICATION_JSON_VALUE,produces = MediaType.APPLICATION_JSON_VALUE)
    public ResposeVO<OdrOrder> queryOdrOrderDetails(/*@ApiParam(value = "业务流水号", required = true,name = "x-b3-traceId") @RequestHeader("x-b3-traceId") String traceId,
                                                    @ApiParam(value = "请求流水号", required = true,name = "x-b3-spanId") @RequestHeader("x-b3-spanId") String spanId,*/
            Principal principal,@RequestBody @ApiParam(value = "订单详情查询请求报文", required = true) @Valid QueryOdrOrderDetailsReqVO request) {
        log.info("订单提交请求报文：{}",request);
        if(principal==null){
            return ResposeVO.unauthorized();
        }
        String url = HOST_MALL_ORDER +PathConstants.ODR_ORDRE + PathConstants.ODR_ORDRE_DETAILS_QUERY;
        ResponseEntity<ResposeVO> responseEntity = restTemplate.postForEntity(url, request, ResposeVO.class);
        return responseEntity.getBody();
    }

    @ApiOperation(value = "订单列表查询", notes = "订单列表查询", position = 4)
    @PostMapping(value = PathConstants.ODR_ORDRE_LIST_QUERY,consumes = MediaType.APPLICATION_JSON_VALUE,produces = MediaType.APPLICATION_JSON_VALUE)
    public ResposeListVO<OdrOrder> queryOdrOrderList(/*@ApiParam(value = "业务流水号", required = true,name = "x-b3-traceId") @RequestHeader("x-b3-traceId") String traceId,
                                                               @ApiParam(value = "请求流水号", required = true,name = "x-b3-spanId") @RequestHeader("x-b3-spanId") String spanId,*/
            Principal principal, @RequestBody @ApiParam(value = "请求报文", required = true) @Valid QueryOdrOrderReqVO request) {
        log.info("订单提交请求报文：{}",request);
        if(principal==null){
            return ResposeListVO.unauthorized();
        }
        UmsMember currentMember = getCurrentMember();
        request.setMobile(currentMember.getUsername());
        String url = HOST_MALL_ORDER +PathConstants.ODR_ORDRE + PathConstants.ODR_ORDRE_LIST_QUERY;
        ResponseEntity<ResposeListVO> responseEntity = restTemplate.postForEntity(url, request, ResposeListVO.class);
        ResposeListVO<OdrOrder> body = responseEntity.getBody();
        return body;
    }

    public UmsMember getCurrentMember() {
        SecurityContext ctx = SecurityContextHolder.getContext();
        Authentication auth = ctx.getAuthentication();
        MemberDetails memberDetails = (MemberDetails) auth.getPrincipal();
        return memberDetails.getUmsMember();
    }
}
